package com.duan.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.duan.boot.entity.Task;
import com.duan.boot.mapper.TaskMapper;
import com.duan.boot.service.TaskPlusService;
import com.duan.boot.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Service
public class TaskServiceImpl implements TaskService {

    private final TaskPlusService taskService;

    private final TaskMapper taskMapper;

    @Autowired
    public TaskServiceImpl(TaskPlusService taskPlusService, TaskMapper TaskMapper){
        this.taskService = taskPlusService;
        this.taskMapper = TaskMapper;
    }


    @Override
    public List<Task> getTaskList(){

        return taskMapper.getTaskList();
    }


    @Override
    public Task getById(Long id) {
        return taskMapper.getById(id);
    }

    @Override
    public Task getByName(String name) {
        return taskMapper.getByName(name);
    }

    @Override
    public Integer insertOaTask(Task task){
        return taskMapper.insertOaTask(task);
    }

    @Override
    public List<Task> getByState(Integer state) {
        return taskMapper.getByState(state);
    }

    @Override
    public List<Task> getByIdOrNameOrState(Object idOrName) {
        List<String> list = new ArrayList<>();
        list.add("未开始");
        list.add("进行中");
        list.add("已完成");
        try {
            if (taskMapper.getAllName().contains(idOrName.toString())){
                QueryWrapper<Task> wrapper = new QueryWrapper<>();

                wrapper.like("task_name",idOrName.toString());

                return taskService.list(wrapper);
            }else if (list.contains(idOrName.toString())){
                int i = 0;
                for (String str : list){

                    if (str.equals(idOrName.toString())){
                        break;
                    }i++;
                }
                return taskMapper.getByState(i);
            }else if (taskMapper.getAllId().contains(Integer.parseInt(idOrName.toString()))){
                QueryWrapper<Task> wrapper = new QueryWrapper<>();
                wrapper.like("task_id", Long.parseLong(idOrName.toString()));
                return taskService.list(wrapper);
            }else {
                return null;
            }
        }catch (NumberFormatException e){
            return null;
        }


    }

    @Override
    public Integer getNumberOfTask() {
        return getTaskList().size();
    }

    @Override
    public List<Task> pagedQuery(Integer page, Integer numberOfPage) {
        Map<Integer, List<Task>> map = new HashMap<>();
        //查询总的页数
        int totalPage = 0;
        if (getNumberOfTask()%numberOfPage==0){
            totalPage = getNumberOfTask()/numberOfPage;
        }else {
            totalPage = getNumberOfTask() / numberOfPage + 1;
        }
        //第一层循环就是每一页的下标
        for (int i = 0; i<totalPage; i++){
            //第二层循环就是获得当前页多有的元素
            List<Task> list = new ArrayList<>();
            for (int j = i*numberOfPage;j<numberOfPage+i*numberOfPage; j++){
                if (j==getTaskList().size()){
                    break;
                }
                list.add(getTaskList().get(j));

            }
            map.put(i, list);
        }
        return map.get(page-1);
    }

    @Override
    public Integer pagedQueryPage(Integer numberOfPage) {
        int totalPage = 0;
        if (getNumberOfTask()%numberOfPage==0){
            totalPage = getNumberOfTask()/numberOfPage;
        }else {
            totalPage = getNumberOfTask() / numberOfPage + 1;
        }
        return totalPage;
    }

    @Override
    public Void updateTaskState() {
        List<Task> lists = getTaskList();
        for (Task list : lists){
            LocalDateTime start = list.getTaskStartTime()
                    .toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            LocalDateTime end = list.getTaskEndTime()
                    .toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            if (LocalDateTime.now().isBefore(start)){
                list.setTaskState(0);
            }else if (start.isBefore(LocalDateTime.now()) && LocalDateTime.now().isBefore(end)){
                list.setTaskState(1);
            }else{
                list.setTaskState(2);
            }
            taskService.updateById(list);
        }
        return null;
    }

}
